package runableEx;

/**
 * @author ZhuQi
 * @ClassName Demo
 * @Date 2022/10/18 11:45
 * @Version 1.0
 * @Description TODO
 */
public class Demo {

    public static void main(String[] args) {
        Thread thread1 = new Thread(execute(()-> System.out.println("1111")));
        thread1.start();

        Thread thread2 = new Thread(andThenRun(()-> System.out.println("1111"),()-> System.out.println("2222")));
        thread2.start();
    }

    /**
     * 测试不用抛出异常的 Runnable
     * 核心原理就是 lambda 是一段可以传输的代码
     * @param runnable
     * @return
     */
    public static Runnable execute(Runnable runnable){
        return ()->{
            try {
                runnable.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }

    /**
     * 测试不用抛出异常的两个 Runnable 方法
     * 核心原理就是 lambda 是一段可以传输的代码
     * @param runnable1
     * @param runnable2
     * @return
     */
    public static Runnable andThenRun(Runnable runnable1, Runnable runnable2) {
        return ()->{
            try {
                runnable1.run();
                runnable2.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }
}
